/*
 * homework/linux_os/hdfs_io.c
 *
 * Harry Wei <harryxiyou@gmail.com> (C) 2013
 */ 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hdfs.h"

#define SIZE         (10000000)

struct parameter {
    char *rw;
    uint64_t bs;
    uint64_t size;
};

#if 1
double get_current_time(void) {
    struct timeval t;
    
    if (gettimeofday(&t, NULL)) {
        fprintf(stderr, "Get current time error!\n");
        return -1;
    }
    return (double) (t.tv_sec * 1000000 + t.tv_usec);
}
#endif

int main(int argc, char *argv[]) {
    char *buf = NULL;
    double start = 0UL;
    double end = 0UL;
    int i = 0;

    buf = (char *) malloc(sizeof(char) * (int) SIZE);
    if (NULL == buf)  {
        fprintf(stderr, "Beyond memory!\n");
        exit(EXIT_FAILURE);
    }
    memset(buf, '\0', sizeof(SIZE));
    printf("enter func %s\n", __func__);
    hdfsFS fs = hdfsConnect("default", 2001);
    printf("fs %p\n", (void *) fs);
    hdfsFile file = hdfsOpenFile(fs, "/tmp/test", O_RDONLY, 0, (int) 1, (tSize) 64000000);
//    sprintf(buf, "%s", "Hello World!\n");
    start = get_current_time();
    printf("start: %lf\n", start);
    tSize len1 = hdfsPread(fs, file, (tOffset) 0, buf, (tSize) SIZE);
    if (len1 != (tSize) SIZE) {
        fprintf(stderr, "hdfsWrite error!\n");
        return -1;
    }
#if 0
    tSize len = hdfsWrite(fs, file, buf, (tSize) SIZE);
    if (len != (tSize) SIZE) {
        fprintf(stderr, "hdfsWrite error!\n");
        return -1;
    }
#endif
//    hdfsFlush(fs, file);
    end = get_current_time();
    printf("end: %lf\n", end);
    printf("%lf MB/s \n", (((double) SIZE) / ((end - start) / 1000000.0)) / 1000000.0);
    hdfsCloseFile(fs, file);
    hdfsDisconnect(fs);
    return 0;
}
